//
//  CZabBar.swift
//  WeiBoNew
//
//  Created by 秦川 on 16/5/11.
//  Copyright © 2016年 秦川. All rights reserved.
//

import UIKit
protocol CZabBarDeleget: NSObjectProtocol{
//  点击撰写按钮回调的方法
    func didSelectcomposeButton()
    
}
class CZabBar: UITabBar {
    // MARK:  撰写button
    var composeButtonClouser : (() ->())?
    
    weak var czDelegate: CZabBarDeleget?
    
    private lazy var composeButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "composeButtonClick", forControlEvents: .TouchUpInside)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        button.sizeToFit()
        return button
    }()
    private func setUpUI(){
        addSubview(composeButton)
        
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        composeButton.center.x = frame.size.width * 0.5
        composeButton.center.y = frame.size.height * 0.5
        let itemWidth = frame.size.width/5
        var index = 0
        for childView in subviews {
            
            if childView .isKindOfClass(NSClassFromString("UITabBarButton")!){
                childView.frame.size.width = itemWidth
                childView.frame.origin.x = CGFloat(index) * itemWidth
                index++
                if index == 2 {
                    index++
                }
            }
        }
    }
    
    
    
    // MARK: 点击事件
    @objc private func composeButtonClick() {
        
        print("点击事件")
      composeButtonClouser?()
        
    czDelegate?.didSelectcomposeButton()
        
        
        
    }
    // MARK:  init方法
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    
    
    //  使用xib或者storyboard创建视图的时候会执行
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
